import { request } from '../../utils/request'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    keyword: '',
    searchData: {
      activityList: [],
      courseList: [],
      matchList: [],
      teamList: [],
      userList: [],
      workList: []
    },
    currentTab: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  onChange({ detail }) {
    this.setData({
      currentTab: detail.name
    })
  },

  getList() {
    if (this.data.keyword.length <= 0) {
      wx.showToast({
        title: '请输入搜索内容',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '加载中...'
    })
    request({
      url: '/mini/api/square/search',
      method: 'POST',
      data: {
        keyword: this.data.keyword
      }
    })
      .then((resdata) => {
        if (resdata.code == 200) {
          wx.hideLoading()
          this.setData({
            searchData: resdata.data
          })
          if (
            this.data.searchData.courseList
              .length > 0
          ) {
            this.setData({
              currentTab: 'COURSE'
            })
            return
          }
          if (
            this.data.searchData.workList.length >
            0
          ) {
            this.setData({
              currentTab: 'WORK'
            })
            return
          }
          if (
            this.data.searchData.activityList
              .length > 0
          ) {
            this.setData({
              currentTab: 'ACTIVITY'
            })
            return
          }
          if (
            this.data.searchData.matchList
              .length > 0
          ) {
            this.setData({
              currentTab: 'MATCH'
            })
            return
          }
          if (
            this.data.searchData.teamList.length >
            0
          ) {
            this.setData({
              currentTab: 'TEAM'
            })
            return
          }
          if (
            this.data.searchData.userList.length >
            0
          ) {
            this.setData({
              currentTab: 'PERSON'
            })
            return
          }
        } else {
          wx.showToast({
            title: resdata.message,
            icon: 'none'
          })
          wx.hideLoading()
        }
      })
      .catch(() => {
        wx.hideLoading()
      })
  },

  toCoursesDetail(item) {
    wx.navigateTo({
      url:
        '/packagePlan/coursesDetail/coursesDetail?id=' +
        item.currentTarget.dataset.item.id
    })
  },

  toWorkDetail(item) {
    wx.navigateTo({
      url:
        '/packagePlan/workDetail/workDetail?id=' +
        item.currentTarget.dataset.item.id
    })
  },

  toActivity(item) {
    wx.navigateTo({
      url:
        '/packagePlan/activityDetail/activityDetail?id=' +
        item.currentTarget.dataset.item.id
    })
  },

  toRankingDetail(item) {
    wx.navigateTo({
      url:
        '/packageSquare/rankingDetail/rankingDetail?id=' +
        item.currentTarget.dataset.item.id
    })
  },

  toWork(item) {
    wx.navigateTo({
      url:
        '/packagePlan/workDetail/workDetail?id=' +
        item.currentTarget.dataset.item.id
    })
  },

  signNow(item) {
    const app = getApp()
    request({
      url: '/mini/api/selectTeam/joinGroup',
      method: 'POST',
      data: {
        groupId: Number(
          item.currentTarget.dataset.item.id
        ),
        userId: Number(
          app.globalData.userInfo.userId
        )
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        wx.navigateTo({
          url:
            '/packageMy/chat/chat?conversationID=' +
            'GROUP' +
            item.currentTarget.dataset.item.id
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  toHomePage(item) {
    wx.navigateTo({
      url:
        '/packageMy/personalHomepage/personalHomepage?userId=' +
        item.currentTarget.dataset.userid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
})
